home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13125 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.1 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c,comp.unix.programmer
  4. Subject: Re: Q: '\n' character - strtok trick
  5. Date: Thu, 04 Apr 96 17:46:02 GMT
  6. Organization: none
  7. Message-ID: <828639962snz@genesis.demon.co.uk>
  8. References: <31616F63.481D@lava.weeg.uiowa.edu> <4jrvv3$ask@aimnet.aimnet.com> <828493319snz@genesis.demon.co.uk> <828542474.7672@tertio.demon.co.uk>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <828542474.7672@tertio.demon.co.uk>
  15.            Andrew_Lord@tertio.co.uk "
  16. " writes:
  17.  
  18. >In article <828493319snz@genesis.demon.co.uk>, Lawrence Kirby  wrote:
  19. >>
  20. >>>In article <31616F63.481D@lava.weeg.uiowa.edu>,
  21. >>>Artur Wojdat  <awojdat@lava.weeg.uiowa.edu> wrote:
  22. >>>
  23. >>>>       Is there a function or some sort of way that I could remove '\n' 
  24. >>>>charecter form the end of the string. I'm reading from two files, want to 
  25. >>>>form one line of text and then have it printed out to stdout. I use fgets to
  26. >>>>read from the file and I noticed that it appends newline char at the end.
  27. >>
  28. >>There is a simple trick to remove any trailing newline character in a string
  29. >>typically written by fgets(), say into buffer,  and that is:
  30. >>
  31. >>   strtok(buffer, "\n");
  32. >>
  33. >At the risk or embarassing myself infront someone who probably knows a lot more
  34. >about the C languange than myself -
  35. >
  36. >Although the behaviour of strtok() is well defined I try to avoid it like the
  37. >plague because it uses a static area to maintain state. If another function
  38. >happens to call it between successive invocations then your screwd.
  39.  
  40. That is a problem.
  41.  
  42. >Having said that, it is obvious that this example is a one-off call to strtok
  43. >and that it is described as a 'trick'. And I have to confess that I like it!
  44.  
  45. Right - other calls to strtok() can't affect this one (reentrant calls
  46. aside) but this can affect other calls.
  47.  
  48. >It would have to be commented though because you are using the side-effect of
  49. >a function to achieve you primary objective? Other people (of varying ability)
  50. >may have to maintain it.
  51.  
  52. A good idea.
  53.  
  54. >        strtok(buffer,'\n'); /* Remove '\n' */
  55.  
  56. Don't for get strtok takes 2 string arguments (or one plus one null pointer)
  57. so you need to pass "\n" as thr 2nd argument.
  58.  
  59. >Is a pleasant improvement on my old way of doing it.
  60. >I used to use
  61. >        if ((p = strchr(buffer,'\n')) != NULL ) *p = '\0';
  62. >
  63. >where p is a 'char *' somewhere, but no more!
  64. >
  65. >Would I be correct in saying avoid using strtok to repeatedly extract tokens
  66. >unless you can guarantee that it will NEVER be called elsewhere in the code
  67. >between your own invocations - and such guarantees are hard to make?
  68.  
  69. If youmhave a loop that calls strtok() then you have to be careful what other
  70. functions you call in the loop. However you are guaranteed that "the
  71. implementation shall behave as if no library function calls the strtok
  72. function".
  73.  
  74. -- 
  75. -----------------------------------------
  76. Lawrence Kirby | fred@genesis.demon.co.uk
  77. Wilts, England | 70734.126@compuserve.com
  78. -----------------------------------------
  79.